//
//  PS_MessageViewController.m
//  FinanceSupermarket
//
//  Created by 丹丹 on 2018/12/12.
//  Copyright © 2018年 xuezi. All rights reserved.
//

#import "PS_MessageViewController.h"
#import "PS_MessageTableViewCell.h"
#import "PS_MessageBottomTableViewCell.h"
#import "PS_MessageIndex.h"
@interface PS_MessageViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView * listTableView;
@property (nonatomic, strong)UIView * headerView;
@property (nonatomic, strong)NSMutableArray * listArray;
@end

@implementation PS_MessageViewController

- (NSMutableArray *)listArray{
    
    if (_listArray == nil) {
        
        _listArray = [NSMutableArray array];
    }
    
    return _listArray;
}
- (UIView *)headerView{
    
    if (_headerView == nil) {
        
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,80)];
    }
    return _headerView;
}

- (UITableView *)listTableView{
    
    if (_listTableView == nil) {
        
        _listTableView = [[UITableView alloc]initWithFrame:CGRectMake(0,SafeAreaTopHeight, SCREEN_WIDTH,SCREEN_HEIGHT-SafeAreaBottomHeight-SafeAreaTopHeight-50)];
        _listTableView.dataSource = self;
        _listTableView.delegate = self;
        _listTableView.showsVerticalScrollIndicator= NO;
        _listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _listTableView.scrollEnabled = YES;
        _listTableView.tableHeaderView = self.headerView;
        
    }
    
    return _listTableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.nav_titleLabel.text = @"消息";
    self.nav_backImg.hidden = YES;
    self.nav_rightImg.hidden = NO;
    self.nav_rightButton.hidden = NO;
    self.nav_rightImg.image = [UIImage imageNamed:@""];
    [self.view addSubview:self.listTableView];
    [self getRecentContactsData];
    // Do any additional setup after loading the view.
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return self.listArray.count;
        
    }else{
        
        return 2;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    self.listTableView.rowHeight = UITableViewAutomaticDimension;
    self.listTableView.estimatedRowHeight = 44;
    return self.listTableView.rowHeight;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 30;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return 55;
        
    }else{
        
        return 10 ;
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        
        UIView * backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 55)];
        backView.backgroundColor = [UIColor whiteColor];
        
        UIView * line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
        line.backgroundColor = [UIColor whiteColor];
        [backView addSubview:line];
        
        UILabel * markLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 20, 100, 25)];
        markLabel.text = @"全部评价";
        markLabel.userInteractionEnabled = YES;
        [backView addSubview:markLabel];
        
        UIImageView * arrowIamge = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 15-7, 25, 7, 15)];
        arrowIamge.image = [UIImage imageNamed:@"xiaofanhui"];
        arrowIamge.userInteractionEnabled = YES;
        [backView addSubview:arrowIamge];
        
        UIView * line2 = [[UIView alloc]initWithFrame:CGRectMake(15,54, SCREEN_WIDTH-30, 1)];
        line2.backgroundColor = [UIColor colorWithHexString:@"efefef"];
        [backView addSubview:line2];
        return backView;
    }else{
        
        UIView * backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
        backView.backgroundColor = [UIColor colorWithHexString:@"efefef"];
        return backView;
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath.section == 0) {
        
        
        PS_MessageTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"PS_MessageTableViewCell"];
        if (cell == nil) {
            
            cell = [[[NSBundle mainBundle]loadNibNamed:@"PS_MessageTableViewCell" owner:self options:nil]objectAtIndex:0];
            
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
      
        PS_MessageIndex * model = [self.listArray objectAtIndex:indexPath.row];
        [cell.headImage sd_setImageWithURL:[NSURL URLWithString:model.attrs]];
        cell.nameLabel.text = model.nickname;
        cell.timeLabel.text = model.create_time;
        cell.contentLabel.text = [self textFromBase64String:model.msg];
        return cell;
        
    }else{
        
        PS_MessageBottomTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"PS_MessageBottomTableViewCell"];
        if (cell == nil) {
            
            cell = [[[NSBundle mainBundle]loadNibNamed:@"PS_MessageBottomTableViewCell" owner:self options:nil]objectAtIndex:0];
            
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        NSArray * imageArr = @[@"dinwgei",@"dinwgei"];
        NSArray * titleArr = @[@"附近的人",@"小组"];
        cell.leftImage.image = [UIImage imageNamed:[imageArr objectAtIndex:indexPath.row]];
        cell.titleLabel.text = [titleArr objectAtIndex:indexPath.row];
        return cell;
        
    }
}

- (void)getRecentContactsData{
    
    [Utils post:MESSAGEINDEX parameters:@{@"uid":@"2"} success:^(NSInteger recode, id jsonObject) {
        
        NSArray * dataArray = [jsonObject objectForKey:@"data"];
        for (NSDictionary * dic in dataArray) {
            
            PS_MessageIndex * model = [PS_MessageIndex mj_objectWithKeyValues:dic];
            [self.listArray addObject:model];
        }
        
        [self.listTableView reloadData];
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
}

- (NSString *)textFromBase64String:(NSString *)base64 {
    NSData *data = [[NSData alloc] initWithBase64EncodedString:base64 options:0];
    NSString *text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    return text;
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
